/*
 * @Author: panghu chenjh@datamargin.com
 * @Date: 2024-04-26 17:11:04
 * @LastEditors: panghu chenjh@datamargin.com
 * @LastEditTime: 2024-04-26 17:24:27
 * @FilePath: \zheye\src\components\createMessage.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

import { createApp } from 'vue'
import Message from './Message.vue'
export type MessageType = 'success' | 'error' | 'primary'
let timer: null | number = null
function CreateMessage (message: string, type: MessageType, timeout = 2000) {
  const messageInstance = createApp(Message, {
    type,
    message
  })
  const mountNode = document.createElement('div')
  document.body.appendChild(mountNode)
  messageInstance.mount(mountNode)

  timer && clearTimeout(timer)
  timer = setTimeout(() => {
    messageInstance.unmount()
    document.body.removeChild(mountNode)
  }, timeout)
}

export default CreateMessage
